// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import FastClick from 'fastclick'

import 'vant/lib/index.css'

import App from './App'
import router from './router'
import AlertPlugin from 'vux/src/plugins/alert'
import ToastPlugin from 'vux/src/plugins/toast'
import LoadingPlugin from 'vux/src/plugins/loading'
import './plugin/netease-adapt'
import MYTouch from './plugin/touch.min'
import Api from './api/index' // ajax组件
import useridPlugin from './store/getUserid' // 检测userid
import config from './store/config' // 全局参数文件
// import share from "@/Plugin/share"; //实现微信分享
import store from './store/index'// 自定义store
import Vuex from 'vuex'
import { DatetimePlugin } from 'vux'
// import { Tree, Tag, Select, Option } from 'element-ui';
import preview from 'vue-photo-preview'// 图片预览
import 'vue-photo-preview/dist/skin.css'
import 'video.js/dist/video-js.css'
import MintUI from 'mint-ui'
import 'mint-ui/lib/style.css'
import { InfiniteScroll} from 'mint-ui'
import ElementUI from 'element-ui'
// Vue.use(Select);
// Vue.use(Option);
import ElTreeGrid from 'element-tree-grid'
import ECharts from 'vue-echarts'
import 'echarts/lib/chart/line'
import echarts from 'echarts'
import moment from 'moment'
// import { Tab, Tabs, } from 'vant'
// Vue.use(Tab)
// Vue.use(Tabs)
import '../static/font.css'
import './css/common.css'
import Vant from 'vant'

import * as Echarts5 from 'echarts5'
import 'echarts-liquidfill'

import Calendar from 'vue2-datepick'
// eslint-disable-next-line standard/object-curly-even-spacing
import { getRequest, postRequest} from './utils/request'
// -------------------------------
// import VConsole from 'vconsole'
// const vConsole = new VConsole()
// Vue.use(vConsole)
// --------------------------------
Vue.use(InfiniteScroll)
Vue.use(preview)
// import 'element-ui/lib/theme-chalk/index.css';
Vue.use(ElementUI)
Vue.prototype.$moment = moment
Vue.use(Vant)
Vue.prototype.$echarts5 = Echarts5
Vue.prototype.$GetRequest = getRequest
Vue.prototype.$PostRequest = postRequest

Vue.prototype.$echarts = echarts
Vue.component('chart', ECharts)
Vue.component(ElTreeGrid.name, ElTreeGrid)
Vue.use(DatetimePlugin)
Vue.use(Vuex)
Vue.use(Api)
Vue.use(useridPlugin)
FastClick.attach(document.body)
Vue.config.productionTip = false
Vue.use(AlertPlugin)
Vue.use(ToastPlugin)
Vue.use(LoadingPlugin) // 时间选择组件
Vue.use(Calendar)


router.beforeEach(function (to, from, next) {
  // store.commit('updateLoadingStatus', { isLoading: true })
  if (to.query.title) {
    to.meta.title = to.query.title
    document.title = to.meta.title
  } else if (to.meta.title) {
    document.title = to.meta.title
  }
  // useridPlugin.$user.checkUserid(to, next)

  // 判断是否是微信内核;
  const isWeChat = window.navigator.userAgent.toLocaleLowerCase()
  if (isWeChat.match(/MicroMessenger/i) == 'micromessenger') {
    useridPlugin.$user.checkUserid(to, next)
  } else {
    next()
  }
})
router.onError((error) => {
  const pattern = /Loading chunk (\d)+ failed/g
  const isChunkLoadFailed = error.message.match(pattern)
  const targetPath = router.history.pending.fullPath
  // console.log(targetPath)
  if (isChunkLoadFailed) {
    location.reload()
  }
})
router.afterEach((to, from, next) => {
  window, scrollTo(0, 0)
})
Vue.prototype.$MyTouch = function (element, types, callback) {
  // 参考网址https://www.jianshu.com/p/ef86cc79b49b
  // 参数描述：
  // element   element或string    元素对象、选择器
  // types  string 事件的类型（多为手势事件），可接受多个事件以空格分开；支持原生事件的透传
  // callback    function 事件处理函数, 移除函数与绑定函数必须为同一引用;

  MYTouch.on(element, types, callback)
}

// 应用基础路径
Vue.prototype.Base_Path = config.base_path
// Vue.prototype.$alertShow = config.Zalert
Vue.prototype.$alertShow = function (msg, fun) {
  this.$vux.alert.show({
    // title: '温馨提示',
    content: msg,
    onShow () {
      // console.log('plugin: I\'m showing')
    },
    onHide () {
      if (fun) {
        fun()
      }
      // console.log('plugin: I\'m hiding now')
    }
  })
}

Vue.prototype.$Post = function (url, postObj, success, failure, isShowLoding, text = '加载中') {
  var _this = this
  if (isShowLoding) {
    _this.$vux.loading.show({
      text
    })
  }
  this.$http.post(url, postObj, function (msg) {
    if (isShowLoding) {
      _this.$vux.loading.hide()
    }
    if (msg.status == 'error') {
      // console.log(failure)
      failure(msg)
    } else {
      success(msg)
    }
  }, function (err) {
    if (isShowLoding) {
      _this.$vux.loading.hide()
    }
    failure(err)
  })
}

// 简单的深复制
Vue.prototype.$deepClone = deepClone

function deepClone (currobj) {
  if (typeof currobj !== 'object') {
    return currobj
  }
  if (currobj instanceof Array) {
    var newobj = []
  } else {
    var newobj = {}
  }
  for (var key in currobj) {
    if (typeof currobj[key] !== 'object') {
      // 不是引用类型，则复制值
      newobj[key] = currobj[key]
    } else {
      // 引用类型，则递归遍历复制对象
      newobj[key] = deepClone(currobj[key])
    }
  }
  return newobj
}

Vue.prototype.$gotTime = function () {
  let now = new Date()

  let month = now.getMonth() + 1
  let day = now.getDate()
  let year = now.getFullYear()
  let h = now.getHours()
  let m = now.getMinutes()
  let s = now.getSeconds()
  function t (time) {
    if (time < 10 && time > 0) {
      return '0' + time
    } else if (!time) {
      return '00'
    }
    return time
  }
  return `${t(year)}-${t(month)}-${t(day)} ${t(h)}:${t(m)}:${t(s)}`
}

// input 点击多次才聚焦 解决办法
FastClick.prototype.focus = function (targetElement) {
  var length
  const isiOS = !!navigator.userAgent.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/) // ios终端
  // Issue #160: on iOS 7, some input elements (e.g. date datetime month) throw a vague TypeError on setSelectionRange. These elements don't have an integer value for the selectionStart and selectionEnd properties, but unfortunately that can't be used for detection because accessing the properties also throws a TypeError. Just check the type instead. Filed as Apple bug #15122724.
  if (isiOS && targetElement.setSelectionRange && targetElement.type.indexOf('date') !== 0 && targetElement.type !== 'time' && targetElement.type !== 'month') {
    length = targetElement.value.length
    targetElement.focus()
    targetElement.setSelectionRange(length, length)
  } else {
    targetElement.focus()
  }
}

// if (process.env.NODE_ENV === 'development') {
Vue.config.devtools = true
// } else {
//   Vue.config.devtools = false
// }

/* eslint-disable no-new */
new Vue({
  el: '#app-box',
  router,
  store,
  render: h => h(App)
  // template: '<App/>',
  // components: { App }
})
